package stopwatch;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.awt.AWTException;

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JSpinner;

import junit.extensions.PrivilegedAccessor;
import org.junit.Test;

import database.Database;

import tests.DatabaseTest;
import utility.Constants;

public class StopwatchPanelTest {
	private static long SLEEP = 2000;

	@Test
	public void testActionPerformed() throws NoSuchFieldException, AWTException {
		try{
			Database.init();
			Thread.sleep(SLEEP);
			Constants.updateActivitiesList();
			int n = Constants.activities.length;
			StopwatchPanel testee;
			JButton keyBtn;
			JSpinner hhDisplay, mmDisplay;
			JComponent comments;
			JSpinner alarmIntervalSelector;
			Thread.sleep(SLEEP);

			for (int i=n-1; i>=0; i--){
				Thread.sleep(SLEEP);
				testee = new StopwatchPanel();
				Thread.sleep(SLEEP);
				assertNotNull(Constants.activitiesList);
				Thread.sleep(SLEEP);
				
				Constants.activitiesList.setSelectedIndex(i);
				Thread.sleep(SLEEP);
				assertEquals(PrivilegedAccessor.getValue(testee, "prevActivityId"), Constants.activitiesList.getSelectedIndex()+1);
				assertNotNull(PrivilegedAccessor.getValue(testee, "stopwatch"));

				keyBtn = (JButton) PrivilegedAccessor.getValue(testee, "keyBtn");
				hhDisplay = (JSpinner) PrivilegedAccessor.getValue(testee, "hhDisplay");
				mmDisplay = (JSpinner) PrivilegedAccessor.getValue(testee, "mmDisplay");
				
				comments = ((JComponent)  PrivilegedAccessor.getValue(testee, "comments"));
				alarmIntervalSelector = (JSpinner) PrivilegedAccessor.getValue(testee, "alarmIntervalSelector");
				Thread.sleep(SLEEP);
				assertTrue( keyBtn.isVisible() && Constants.activitiesList.isVisible()  && 
						!hhDisplay.isEnabled()  && !mmDisplay.isEnabled()  &&
						 !comments.isVisible() );
				Thread.sleep(SLEEP);
				assertEquals(keyBtn.getText(), PrivilegedAccessor.getValue(testee, "PAUSE"));
				
				
				keyBtn.doClick(); //pause
				
				assertTrue((Integer) mmDisplay.getValue() < 1);
				Thread.sleep(SLEEP);
				
				keyBtn.doClick(); //resume
				Thread.sleep(Constants.MM_MILLIS*2);
				keyBtn.doClick();// pause
				int mmEl = (Integer) mmDisplay.getValue();
				assertEquals(mmEl,2);
				
				Thread.sleep(SLEEP);
				alarmIntervalSelector.setValue(1);
				keyBtn.doClick();  // resume
				
				Thread.sleep(Constants.MM_MILLIS);
				keyBtn.doClick(); // pause
				mmEl = (Integer) mmDisplay.getValue();
				assertEquals(mmEl,3);
				
				mmDisplay.setValue(50);
				
				mmEl = (Integer) mmDisplay.getValue();
				assertEquals(mmEl,50);
				
				Thread.sleep(Constants.MM_MILLIS);
				keyBtn.doClick(); // resume
				mmEl = (Integer) mmDisplay.getValue();
				assertEquals(mmEl,50);
				Thread.sleep(Constants.MM_MILLIS);
				mmEl = (Integer) mmDisplay.getValue();
				assertEquals(mmEl,51);
				
				
				keyBtn.doClick(); // pause
				mmEl = (Integer) mmDisplay.getValue();
				assertEquals(mmEl,51);
				Thread.sleep(Constants.MM_MILLIS);
				mmEl = (Integer) mmDisplay.getValue();
				assertEquals(mmEl,51);
				
				keyBtn.doClick(); // resume
				mmEl = (Integer) mmDisplay.getValue();
				assertEquals(mmEl,51);
				Thread.sleep(SLEEP);
				keyBtn.doClick();
				mmEl = (Integer) mmDisplay.getValue();
				assertEquals(mmEl,51);
			}
			Thread.sleep(SLEEP);
			PrivilegedAccessor.invokeMethod(DatabaseTest.class, "dropTables()");
		} catch (Exception e){
			e.printStackTrace();
			fail();
		}
	}

	@Test
	public void testStopwatchPanel() throws NoSuchFieldException {
		try {
			Database.init();

			final StopwatchPanel testee = new StopwatchPanel();
			assertNull(PrivilegedAccessor.getValue(testee, "stopwatch"));
			assertNotNull(PrivilegedAccessor.getValue(testee, "alarmIntervalSelector"));
			assertTrue( !((JComponent) PrivilegedAccessor.getValue(testee, "keyBtn")).isVisible() && 
					
					!((JComponent) PrivilegedAccessor.getValue(testee, "hhDisplay")).isVisible() &&
					!((JComponent) PrivilegedAccessor.getValue(testee, "mmDisplay")).isVisible() &&
					Constants.activitiesList.isVisible());

			PrivilegedAccessor.invokeMethod(DatabaseTest.class, "dropTables()");


		} catch (Exception e) {
			fail();
			e.printStackTrace();
		}
	}
}
